DRIVERS_C := $(wildcard drivers/*.c)
COMMON_C := $(wildcard common/*.c)
LOGIC_C := $(wildcard logic/*.c)
ZIGBEE_C := $(wildcard zigbee/*.c)
UI_C := $(wildcard ui/*.c)
SOURCES := $(DRIVERS_C) $(COMMON_C) $(LOGIC_C) $(UI_C) $(ZIGBEE_C)
OBJECTS := $(patsubst %.c,objects/%.o,$(SOURCES))
DEPS := $(patsubst %.c,objects/%.d,$(SOURCES))

OUTPUT_ELF := debug/loadrtu_msp430.elf
OUTPUT_A43 := debug/loadrtu_msp430.a43
OUTPUT_LST := debug/loadrtu_msp430.lst

CC := msp430-gcc
OBJCOPY := msp430-objcopy
CFLAGS := -mmcu=msp430f1611 -O2 -Wall -g -Icommon -Idrivers -Ilogic -Iui -Izigbee
OBJDUMP := msp430-objdump

all: $(OUTPUT_A43)

objects/%.o: %.c
	@mkdir -p `dirname $@`
	$(CC) -c $< -o $@ $(CFLAGS)

objects/%.d: %.c
	@mkdir -p `dirname $@`
	$(CC) $(CFLAGS) -MF"$@" -MG -MM -MP -MT"$@" -MT"$(patsubst %.c,objects/%.o,$<)" "$<"

include $(DEPS)

$(OUTPUT_ELF): $(OBJECTS)
	@mkdir -p debug
	$(CC) $+ -o $@ $(CFLAGS)

$(OUTPUT_A43): $(OUTPUT_ELF)
	$(OBJCOPY) -O ihex $+ $@
	$(OBJDUMP) -dSt $+ > $(OUTPUT_LST)

clean:
	rm -rf objects debug

